package org.summerframework.component.security.role.controller;

import org.summerframework.component.security.annotion.RoleRequired;
import org.summerframework.component.security.controller.BaseController;
import org.summerframework.component.security.role.service.UserRoleService;
import org.summerframework.component.security.role.service.dto.UserRoleDTO;
import org.summerframework.component.security.role.service.dto.UserRoleQueryDTO;
import org.summerframework.core.base.result.ResultDTO;
import org.summerframework.core.page.PageResult;
import org.summerframework.core.util.OptionalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "角色管理", description = "功能描述")
@RequestMapping("user/role")
public class UserRoleController extends BaseController {
    @Resource
    private UserRoleService userRoleService;

    @PostMapping
    @ApiOperation("添加")
    @RoleRequired(name = "角色管理-添加", code = "ROLES_ADD")
    public ResultDTO<Boolean> insert(UserRoleDTO userRoleDTO) {
        return ResultDTO.ok(true, userRoleService.insert(userRoleDTO));
    }

    @PutMapping
    @ApiOperation("更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long", paramType = "query")})
    @RoleRequired(name = "角色管理-更新", code = "ROLES_UPDATE")
    public ResultDTO<Integer> updateById(UserRoleDTO userRoleDTO) {
        return ResultDTO.ok(userRoleService.updateById(userRoleDTO));
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long", paramType = "path")})
    @RoleRequired(name = "角色管理-删除", code = "ROLES_DELETE")
    public ResultDTO<Integer> deleteById(@PathVariable("id") Long id) {
        return ResultDTO.ok(userRoleService.deleteById(id));
    }

    @DeleteMapping
    @ApiOperation("根据id列表删除删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "根据ids删除", required = true, dataType = "List", paramType = "query")})
    @RoleRequired(name = "角色管理-删除", code = "ROLES_DELETE")
    public ResultDTO<Integer> deleteByIds(@RequestParam("ids") List<Long> ids) {
        return ResultDTO.ok(userRoleService.deleteByIds(ids));
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long", paramType = "path")})
    @RoleRequired(name = "角色管理-查询", code = "ROLES_SELECT")
    public ResultDTO<UserRoleDTO> selectById(@PathVariable("id") Long id) {
        return ResultDTO.ok(OptionalUtils.get(userRoleService.selectById(id)));
    }

    @GetMapping
    @ApiOperation("根据query查询")
    @RoleRequired(name = "角色管理-查询", code = "ROLES_SELECT")
    public PageResult<UserRoleDTO> selectPageResultByQuery(UserRoleQueryDTO userRoleQueryDTO) {
        return userRoleService.selectPageResultByQuery(userRoleQueryDTO);
    }
}
